// prisma-exception.filter.ts
import { ExceptionFilter, Catch, ArgumentsHost } from '@nestjs/common'
import { Prisma } from '@prisma/client'

@Catch(Prisma.PrismaClientKnownRequestError)
export class PrismaExceptionFilter implements ExceptionFilter {
  catch(exception: Prisma.PrismaClientKnownRequestError, host: ArgumentsHost) {
    const ctx = host.switchToHttp()
    const response = ctx.getResponse()

    if (exception.code === 'P2002') {
      console.log(exception, 'exception')
      response.status(409).json({
        statusCode: 409,
        message: '数据已存在，请检查输入。',
      })
    } else {
      response.status(500).json({
        statusCode: 500,
        message: '数据库操作失败',
      })
    }
  }
}
